
if(!window._$)window._$ = function(id){return document.getElementById(id)} 
  function check(){
        var form=document.forms[0];
        var custom = '';
        var info = '';
        for(var i=0;i<mainTbl.rows.length;i++){
        	for(var j=0;j<mainTbl.rows[i].cells.length;j++){
        		var cell = mainTbl.rows[i].cells[j];
        		var cid = $(cell).find('select')[0].value;
        		if(cid==''){
        			info += '\n第'+(i+1)+'行'+(j+1)+'列';
        		}
        		custom += cid+':row='+i+',col='+j+',colSpan='+cell.colSpan+';';
        	}
        }
        if(info!=''){
        	alert('以下版块尚未选择栏目：'+info);
        	return false;
        }
        form.custom.value = custom;
        return true;
    }
    function selRadio(newCols){
    	if(newCols==mainTbl.cols)return;
    	var cell = null;
    	if(newCols<mainTbl.cols){
    		if(!confirm("您选择了"+newCols+"列样式，原来是"+mainTbl.cols+"列，此操作会将最右边列中的栏目删除掉，是否继续？")){
    			_$('rad'+newCols).checked=false;
    			_$('rad'+mainTbl.cols).checked=true;
    			return;
    		}
    		for(var i=0;i<mainTbl.rows.length;i++){
    			var row = mainTbl.rows[i];
    			var cellNum = mainTbl.cols;
    			while(cellNum>newCols){
	    			cell = row.cells[row.cells.length-1];
	    			if(cell.colSpan>1){
	    				cell.colSpan--;
	    			}
	    			else{
	    				row.deleteCell(row.cells.length-1);
	    			}
	    			cellNum --;
	    		}
	    		for(var j=0;j<row.cells.length;j++){
	    			if($(row.cells[j]).attr('search')=='false'){
	    				var ext = newCols;
	    				if(cell.colSpan>1)ext=cell.colSpan+'_'+newCols;
	    				row.cells[j].firstChild.rows[1].cells[0].innerHTML = '<img src="'+path+'/images/sys/info/news'+ext+'.png" border=0/>';
	    			}
	    		}
    		}
    	}
    	else{
    		for(var i=0;i<mainTbl.rows.length;i++){
    			var row = mainTbl.rows[i];
    			var cellNum = 0;
    			for(var j=0;j<row.cells.length;j++)cellNum += row.cells[j].colSpan;
    			for(var j=cellNum;j<newCols;j++){
	    			cell = row.cells[row.cells.length-1];
	    			if(($(cell).attr('search')!='false'&&cell.colSpan==cellNum) || cell.colSpan>1){//如果是搜索框已占用了整行或者合并列的栏目，直接占用一行的空间
	    				cell.colSpan=newCols;
	    			}
	    			else{
	    				cell = row.insertCell(-1);
	    				initNewCell(cell);
	    			}
    			}
	    		for(var j=0;j<row.cells.length;j++){//修改列的百分比
	    			if(row.cells[j].colSpan==1){
	    				row.cells[j].style.width = parseInt(100/newCols)+"%";
	    			}
	    			cell = row.cells[j];
	    			//cell.
	    			//刷新新闻内容中的图片
    				var ext = 'news'+newCols;
    				if(cell.colSpan>1)ext='news'+cell.colSpan+'_'+newCols;
    				if($(cell).attr('search')!='false')ext='search'
    				cell.firstChild.rows[1].cells[0].innerHTML = '<img src="'+path+'/images/sys/info/'+ext+'.png" border=0/>';
	    		}
    		}
    	}
    	mainTbl.cols = newCols;
    	document.getElementById('rad'+newCols).checked = true;
    }
    function initNewCell(td){
		td.innerHTML = newsTblHtml;
		$(td).css('vertical-align', 'top');
		var tmp = td.firstChild.rows[0].cells[0].children[0].lastChild;
		tmp.innerHTML = _$('colSelectHtml').innerHTML;
		tmp.children[0].value = getFreeColumn();
		$(td).attr({'search':tmp.children[0].value=='search_column'});
		if(tmp.children[0].value=='search_column'){
			$(td).find('tbody td').html('<div class="search-div" style="width:'+(td.colSpan*768/getCols(td)-24)+'px"><img src="'+path+'/images/sys/info/search.png"/></div>');
		}
		else{
			$(td).find('tbody td').html('<img src="'+path+'/images/sys/info/news'+mainTbl.cols+'.png"/>');
		}
    }
    function getFreeColumn(){
    	var eles = document.getElementsByName("colId");
    	var sel = _$('colSelectHtml').children[0];
    	for(var i=0;i<sel.options.length;i++){
    		var tmp = false;
    		for(var j=0;j<eles.length;j++){
    			if(eles[j]!=sel&&eles[j].value==sel.options[i].value){tmp = true;break;}
    		}
    		if(!tmp)return sel.options[i].value;
    	}
    	return 'search_column';
    }
    function changeSel(){
    	var div = event.srcElement.parentNode;
    	if(div.info=='search'){
    		
    	}
    }
    function deleteRow(obj){
    	if(obj==null)return;
    	while(!$(obj).attr('search'))obj = obj.parentNode;
    	var index = obj.parentNode.rowIndex;
    	if(!confirm('确定要删除本行吗？'))return;
    	mainTbl.deleteRow(index);
    }
    function splitRow(obj){
    	if(obj==null)return;
    	var aa = obj.parentNode;
    	while(!$(obj).attr('search'))obj = obj.parentNode;
    	var index = obj.parentNode.rowIndex;
    	row = mainTbl.rows[index];
    	cell = row.cells[0];
    	if($(cell).attr('search')=='true'){
    		alert('搜索框所在行不能拆分!');
    		return;
    	}
    	if(!confirm('确定要拆分本行吗？'))return;
    	for(var i=1;i<cell.colSpan;i++){
    		var tmp = row.insertCell(-1);
    		initNewCell(tmp);
    	}
    	cell.colSpan=1;
  		for(var j=0;j<row.cells.length;j++){
  			if($(row.cells[j]).attr('search')=='false'){
  				row.cells[j].style.width = parseInt(768/mainTbl.cols)+'px';
  				row.cells[j].firstChild.rows[1].cells[0].innerHTML = '<img src="'+path+'/images/sys/info/news'+mainTbl.cols+'.png" border=0/>';
  			}
  		}
  		aa.innerHTML = "<a href='javascript:empty();' onclick='combinRow(this)' title='合并成一列'><img border=0 width=14 height=13 src='"+path+
  				"/images/sys/info/right.png' style='cursor:pointer;'/></a>";
    }
    function combinRow(obj){
    	if(obj==null)return;
    	var aa = obj.parentNode;
    	while(obj&&!$(obj).attr('search'))obj = obj.parentNode;
    	if(!obj){
    		return;
    	}
    	var index = obj.parentNode.rowIndex;
    	row = mainTbl.rows[index];
    	cell = row.cells[0];
    	if(!confirm('确定要合并本行吗？'))return;
    	for(var i=1;i<row.cells.length;i++){
    		row.deleteCell(i);
    	}
    	cell.colSpan = mainTbl.cols;
    	cell.style.width = '100%';
    	if($(cell).attr('search')=='false'){
      		cell.firstChild.rows[1].cells[0].innerHTML = '<img src="'+path+'/images/sys/info/news1.png" border=0/>';
    	}
    	else{
      		cell.firstChild.rows[1].cells[0].innerHTML = '<div class="search-div" style="width:'+(768-24)+'px;"><img src="'+path+'/images/sys/info/search.png" border=0/></div>';
    	}
  		aa.innerHTML = "<a href='javascript:empty();' onclick='splitRow(this)' title='拆分成多列'><img border=0 width=14 height=13 src='"+path+
  				"/images/sys/info/left.png' style='cursor:pointer;'/></a>";
    }
    var newsTblHtml = '<table width="100%" border="0" cellpadding="0" cellspacing="0" class="maintable">'+
'<thead><tr><td colspan="4" nowrap><div class="col-div" info="search" id="">'+
'<a class="icoinfo" style="margin-top:5px;"></a><div style="text-align:left;"></div></div></td></tr></thead>'+
'<tbody><tr><td colspan="4" valign=middle></td></tr></tbody></table>';
    
    function empty(){}
    
    function addRow(){
    	var row = mainTbl.insertRow(-1);
    	for(var i=0;i<mainTbl.cols;i++){
    		var cell = row.insertCell(-1);
    		initNewCell(cell);
    		if(i==0){
    			var span = document.createElement("span");
    			span.innerHTML = "<span><a href='javascript:empty();' onclick='combinRow(this)' title='合并成一列'>"+
    					"<img border=0 width=14 height=13 src='"+path+"/images/sys/info/right.png' style='cursor:pointer;'/></a></span>&nbsp;"+
    					"<a href='javascript:void(0);' onclick='deleteRow(this)' title='删除行'><img border=0 width=14 height=13 src='"+path+
    					"/images/sys/info/delete_box.png' style='cursor:pointer;'/></a>"
    			cell.firstChild.rows[0].cells[0].firstChild.lastChild.appendChild(span);
    		}
  			cell.style.width = parseInt(100/mainTbl.cols)+"%";
  			//cell.children[0].rows[1].cells[0].innerHTML = '<img src="'+path+'/images/sys/info/news'+mainTbl.cols+'.png" border=0/>';
    	}
    }
    
    function selColumnId(obj){
    	
    	var eles = document.getElementsByName("colId");
    	var sel = _$('colSelectHtml').firstChild;
    	if(obj.value!='search_column'){
			var tmp = false;
			for(var j=0;j<eles.length;j++){
				if(eles[j].value==obj.value && eles[j]!=obj){tmp = eles[j];break;}
			}
			if(tmp){
				if(confirm('栏目'+obj.options[obj.selectedIndex].text+'已经存在，是否与['+obj.options[obj.selectedIndex].text+']互换位置?')){
					obj.value = tmp.value;tmp.value = obj.oldValue;tmp.oldValue=tmp.value;
				}
				else{
					obj.value = obj.oldValue;
				}
			}
    	}
		var td = obj;
    	while(!$(td).attr('search'))td = td.parentNode;
		if(obj.value=='search_column'){
			$(td).attr('search','true');
			td.firstChild.rows[1].cells[0].innerHTML = '<div class="search-div" style="width:'+(td.colSpan*768/getCols(td)-24)+'px"><img src="'+path+'/images/sys/info/search.png" border=0/></div>';
		}
		else{
			$(td).attr('search','false');
			var ext = mainTbl.cols;
			if(td.colSpan>1)ext=td.colSpan+'_'+mainTbl.cols;
			td.firstChild.rows[1].cells[0].innerHTML = '<img src="'+path+'/images/sys/info/news'+ext+'.png" border=0/>';
		}
		obj.oldValue = obj.value;
    }
    
    function getCols(cell){
    	var tr = cell.parentNode;
    	var sum = 0;
    	for(var i=0;i<tr.cells.length;i++){
    		sum += tr.cells[i].colSpan;
    	}
    	return sum;
    }